/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhnb
 * Date: 2011-8-2 下午01:14:14
 */
package com.number.box;

import java.io.IOException;
import java.io.Serializable;

import Ice.Application;

import com.number.box.MyBoxPrx;
import com.number.box.MyBoxPrxHelper;

public class MyBoxClient extends Application implements Serializable {

    private static final long serialVersionUID = -4071665198631360834L;

    public static void main(String[] args) {
        
System.out.println("MyBoxClient started.");
        MyBoxClient myBoxClient = new MyBoxClient();
        int status = myBoxClient.main("MyBoxClient", args,
                "com/number/box/config.client");
        System.exit(status);
    }

    @Override
    public int run(String[] args) {

System.out.println(this.getClass()+ " run()...");

        // if args.length > 0, so the inner parameters be have unknown elements.
        if (args.length > 0) {
            System.err.println(appName() + ": too many parameters");
            return 1;
        }

        // set a hook, to cleanup the run-time environment.
        setInterruptHook(new ShutdownHook());

        // * ice_twoway() is a new Back Connection, this connection created on
        // other connection which created by RPC from Client to Server. And
        // now we can use Glance2 replace it.
        MyBoxPrx myBoxPrx = MyBoxPrxHelper.checkedCast(communicator()
                .propertyToProxy("MyBox.Proxy").ice_twoway().ice_timeout(-1)
                .ice_secure(false));

        if (myBoxPrx == null) {
            System.err.println("invalid object proxy.");
            return -1;
        }

        // various proxy.
        MyBoxPrx oneway = MyBoxPrxHelper.uncheckedCast(myBoxPrx.ice_oneway());
        MyBoxPrx datagram = MyBoxPrxHelper.uncheckedCast(myBoxPrx
                .ice_datagram());
        MyBoxPrx batchOneway = MyBoxPrxHelper.uncheckedCast(myBoxPrx
                .ice_batchOneway());
        MyBoxPrx batchDatagram = MyBoxPrxHelper.uncheckedCast(myBoxPrx
                .ice_batchDatagram());

        menu();

        // a reader.
        java.io.BufferedReader buffReader = new java.io.BufferedReader(
                new java.io.InputStreamReader(System.in));

        // choice the pattern operation.
        String line = null;
        do {
            System.out.println("$");
            System.out.flush();
            try {
                line = buffReader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (line.equals("t")) {
                myBoxPrx.echo(line);
            } 
            /*
            // please reference 'README'>FAQ>2 for information about \
            // the 'Ice.TwowayOnlyException'
            else if (line.equals("o")) {
                oneway.echo(line);
            } else if (line.equals("O")) {
                batchOneway.echo(line);
            } else if (line.equals("d")) {
                datagram.echo(line);
            } else if (line.equals("D")) {
                batchDatagram.echo(line);
            } else if (line.equals("f")) {
                communicator().flushBatchRequests();
            } 
            */
            else if (line.equals("?")) {
                menu();
            } else {
                System.out.println("unknown command '" + line + "'");
                menu();
            }
        } while (!line.equals("x"));

        System.out.println("Thanks.");

        return 0;
    }

    class ShutdownHook extends Thread {

        @Override
        public void run() {
            communicator().destroy();
        }

    }

    private void menu() {
        System.out.println("usage:\n" + "t: send greeting as twoway\n"
                + "o: send greeting as oneway\n"
                + "O: send greeting as batch oneway\n"
                + "d: send greeting as datagram\n"
                + "D: send greeting as batch datagram\n"
                + "f: flush all batch requests\n" + "x: exit\n" + "?: help\n");
    }
}
